package br.com.projeto.activity;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Handler.Callback;
import android.os.Message;
import android.provider.Settings;
import android.widget.Toast;
import br.com.projeto.MegaSenaApplication;

import com.tbm.megasena.R;

public class BaseActivity extends Activity implements Callback  {

	protected String TAG;
	
	protected DialogInterface.OnClickListener internetDialogClickListener;
	
	private ProgressDialog progressDialog;

	public BaseActivity() {
		this.internetDialogClickListener = new DialogInterface.OnClickListener() {
			@Override
			public void onClick(DialogInterface dialog, int which) {
				switch (which) {
				case DialogInterface.BUTTON_POSITIVE:
					BaseActivity.this.startActivity(new Intent(Settings.ACTION_WIRELESS_SETTINGS));
					break;
				case DialogInterface.BUTTON_NEGATIVE:
					break;
				}
			}
		};
	}

	@Override
	public boolean handleMessage(Message msg) {
		this.dismissProgressDialog();
		switch (msg.what) {
		case MegaSenaApplication.HANDLE_APPLICATION_OFFLINE:
			new AlertDialog.Builder(this)
				.setMessage(R.string.connection_not_found)
				.setPositiveButton(R.string.option_yes, internetDialogClickListener)
				.setNegativeButton(R.string.option_no, internetDialogClickListener)
				.setCancelable(false).show();
			break;

		case MegaSenaApplication.HANDLE_SERVER_ERRO:
			Toast.makeText(this.getApplicationContext(), this.getString(R.string.server_error), Toast.LENGTH_SHORT).show();
			break;
		}
		return true;
	}
	
	public synchronized final void showProgressDialog(String message) {
		this.dismissProgressDialog();
		progressDialog = ProgressDialog.show(this, this.getString(R.string.wait), message);
	}

	public synchronized final void dismissProgressDialog() {
		if (progressDialog != null) {
			progressDialog.dismiss();
			progressDialog = null;
		}
	}
}
